package actions 
{
	import com.greensock.TweenLite;
	import core.App;
	import flash.display.Graphics;
	import flash.display.Shape;
	import view.scene.Scene;
	/**
	 * ...
	 * @author Johnny Cheng
	 */
	public class VerticalBlindsAction 
	{
		public var p:Number = 0;
		
		private var _oldScene:Scene;
		private var _newScene:Scene;
		private var _oldMasker:Shape;
		private var _newMasker:Shape;
		
		public function VerticalBlindsAction(oldScene:Scene, newScene:Scene) 
		{
			if (oldScene == null) return;
			oldScene.mouseChildren = newScene.mouseChildren = false;
			
			_oldScene = oldScene;
			_newScene = newScene;
			
			_oldMasker = new Shape();
			_oldScene.mask = _oldMasker;
			_oldScene.addChild(_oldMasker);
			
			_newMasker = new Shape();
			_newScene.mask = _newMasker;
			_newScene.addChild(_newMasker);
			
			TweenLite.to(this, 0.8, { p:1, onUpdate:onUpdate, onComplete:onComplete } );
		}
		
		private function onUpdate():void 
		{
			drawMasker(_oldMasker.graphics, true);
			drawMasker(_newMasker.graphics);
		}
		
		private function onComplete():void 
		{
			_oldScene.removeChild(_oldMasker);
			_oldMasker = null;
			_oldScene.mask = null;
			_oldScene.removeSelf();
			
			_newScene.removeChild(_newMasker);
			_newMasker = null;
			_newScene.mask = null;
			_newScene.mouseChildren = true;
		}
		
		private function drawMasker(g:Graphics, hiding:Boolean = false):void
		{
			var trips:int = 10;
			
			g.clear();
			g.beginFill(0);
			for (var i:int = 0; i < trips; i++) {
				if (hiding) g.drawRect(0, (i + p) * (App.stage.stageHeight / trips), App.stage.stageWidth, (1 - p) * App.stage.stageHeight / trips);
				else g.drawRect(0, App.stage.stageHeight * i / trips, App.stage.stageWidth, p * App.stage.stageHeight / trips);
			}
			g.endFill();
		}
	}

}